package com.anmi.transfer.table.concrete;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.anmi.alfred.service.concreteservice.OrderIndexService;
import com.anmi.transfer.entity.Order;
import com.anmi.transfer.service.OrderService;
import com.anmi.transfer.table.base.AbstractSyncTable;
import com.anmi.transfer.util.SpringContextUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderSync extends AbstractSyncTable {

  // 所有子类必须为该成员变量赋值
  {
    TABLE_NAME = "t_order";
  }

  @Override
  public void handleData(List<CanalEntry.Entry> entrys) {
    super.handleData(entrys);
  }

  @Override
  public void insert(CanalEntry.Column column) {
    OrderService orderService = SpringContextUtil.getBean(OrderService.class);
    Order order = orderService.findById(Long.valueOf(column.getValue()));
    if (order != null) {
      String json = JSON.toJSONString(order);
      OrderIndexService orderIndexService = SpringContextUtil.getBean(OrderIndexService.class);
      orderIndexService.index(Order.INDEX_NAME, order.getId().toString(), json);
    }
  }

  @Override
  public void update(CanalEntry.Column column) {
    this.insert(column);
  }
}
